import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        try {
            while (true) {
                System.out.println("\n请选择以下操作：");
                System.out.println("[1、Excel转XML/JSON]");
                System.out.println("[2、XML文件管理]");
                System.out.println("[3、XML转数据库]");
                System.out.println("[exit、退出]");
                System.out.print("请输入: ");
                String choice = scanner.nextLine().trim();

                switch (choice) {
                    case "1":
                        // 直接调用Excel转换工具的手动转换方法
                        ExcelToXmlConverter.manualConvert();
                        break;
                    case "2":
                        XMLStudentManager.main(new String[]{});
                        break;
                    case "3":
                        handleXmlToDbConversion();
                        break;
                    case "exit":
                        System.out.println("系统已退出");
                        return;
                    default:
                        System.out.println("无效选择，请重新输入");
                }
            }
        } finally {
            scanner.close();
        }
    }

    private static void handleXmlToDbConversion() {
        System.out.print("请输入XML文件路径（绝对路径）: ");
        String xmlPath = scanner.nextLine().trim();

        // 检查文件扩展名
        if (!xmlPath.toLowerCase().endsWith(".xml")) {
            System.err.println("错误：必须输入XML文件路径（.xml后缀）");
            return;
        }

        try {
            XMLtoDB.setXmlFilePath(xmlPath);
            XMLtoDB converter = new XMLtoDB();
            converter.convertXmlToDb();
            System.out.println("数据已成功导入数据库");
        } catch (Exception e) {
            System.err.println("数据库转换失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}